package com.bestom.t9mod.core.api;

import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.bestom.t9mod.base.manager.Serial.SerialManager;
import com.bestom.t9mod.base.service.SerialPortService;
import com.bestom.t9mod.commons.exceptions.SerialException;
import com.bestom.t9mod.commons.utils.DataConversion;
import com.bestom.t9mod.commons.utils.DataTurn;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android_serialport_api.SerialPort;

import static android.content.ContentValues.TAG;

/**
 * 电源管理 api
 */
public class PowerApi {

    private static final String TAG = "PowerApi";
    private static int decimal;
    private DataConversion mDataConversion = new DataConversion();
    public static SerialPort mSerialPort;
    private InputStream mInputStream;
    private OutputStream mOutputStream;
    private DataTurn mDataTurn = new DataTurn();

    public void initService(Context context){
        Intent service = new Intent(context,SerialPortService.class);
        context.startService(service);
    }

    public boolean serialIsOpen(){

        if (mSerialPort == null)
            return false;
        // 获取输入输出流
        mInputStream = mSerialPort.getInputStream();
        mOutputStream = mSerialPort.getOutputStream();
        return true;
    }

    public int getPowerState(){
        return decimal;
    }

    public boolean openAllPower(){
        boolean success = sendHex("553a0502ff95");
        if (success)
            decimal = 255;
        return success;
    }

    public boolean closeAllPower(){
        boolean success = sendHex("553a05020096");
        if (success)
            decimal = 0;
        return success;
    }

    public void closeBoardPower(){
        sendHex("553a040093");
    }

    public boolean openSinglePower(int locate){

        if (locate < 0 || locate > 7)
            return false;
        int data = mDataConversion.computRegByteData(decimal,locate);
        boolean success = sendHex(combinationHex(data));
        if (success){
            decimal = data;
            return true;
        }
        return false;
    }

    public boolean closeSinglePower(int locate){

        if (locate < 0 || locate > 7)
            return false;
        int data = mDataConversion.computRegOpenByteData(decimal,locate);
        boolean success = sendHex(combinationHex(data));
        if (success){
            decimal = data;
            return true;
        }
        return false;
    }

    private String combinationHex(int data){
        String hexString = Integer.toHexString(data);
        String hex;
        if (hexString.length() == 1){
            hex = "553a05020" + hexString;
        }else {
            hex = "553a0502" + hexString;
        }
        String checksum = mDataConversion.makeChecksum(hex);
        return hex + checksum.substring(checksum.length()-2);
    }

    private boolean sendHex(String stringHex){

        if (serialIsOpen()){

            Log.i(TAG,"sendHex : " + stringHex);

            byte[] bytes = mDataTurn.HexToByteArr(stringHex);
            // 写入数据
            try {
                mOutputStream.write(bytes);
                mOutputStream.flush();
            } catch (IOException e) {
                e.printStackTrace();
                return false;
            }
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return true;
        }
        Log.i(TAG,"串口未打开，无法发送数据 : " + stringHex);

        return false;

    }

    public void setRunTime(Context context, long time){
        if (time < 2000)
            return;
        Intent intent = new Intent("time");
        intent.putExtra("time", time);
        context.sendBroadcast(intent);
    }

    public void stopReadElectric(Context context){
        send(context,-999);
    }

    public void startReadElectric(Context context){
        send(context,999);
    }

    private void send(Context context, int num){
        Intent intent = new Intent("TIMER");
        intent.putExtra("TIMER", num);
        context.sendBroadcast(intent);
    }
}
